/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package normalizer;

import java.util.Arrays;
import java.util.Collection;
import normalizer.constant.NormalizerPropertiesLoader;
import org.apache.commons.lang3.StringEscapeUtils;
import persistence.facade.FacadePersistence;

/**
 *
 * @author Ricardo
 */
public class Normalizer {

    /**
     * @param args the command line arguments
     */
    private TextProcessing textProcessing;
    
    public Normalizer(){
        textProcessing = new TextProcessing();
    }
    public static void main(String[] args) {
        // TODO code application logic here    
    }
    
   
    public void normalize(){

        FacadePersistence facadePersistence = FacadePersistence.getInstance();
        
        Collection<String> filesColl = facadePersistence.listOriginalFiles();
        for (String pathFile : filesColl) {
            String dataFile = facadePersistence.getFileData(pathFile);
            String unEscapeData = this.unescapeHtml(dataFile);
            String cleanData = this.applyPreprocessRegexExpression(unEscapeData);
            facadePersistence.createCleanFile(facadePersistence.getFileName(pathFile), cleanData);
        }
    }
    
    
    public void tokenize(){

        FacadePersistence facadePersistence = FacadePersistence.getInstance();
        Collection<String> filesColl = facadePersistence.listSentencesFiles();
        for (String pathFile : filesColl) {
            String dataFile = facadePersistence.getFileData(pathFile);
            String[] tokenized = this.textProcessing.tokenize(dataFile);
            facadePersistence.createTokenizedFile(facadePersistence.getFileName(pathFile), Arrays.toString(tokenized));
        }
    }
    
    public void sentenceDetector(){
        FacadePersistence facadePersistence = FacadePersistence.getInstance();
        Collection<String> filesColl = facadePersistence.listCleanFiles();
        for (String pathFile : filesColl) {
            String dataFile = facadePersistence.getFileData(pathFile);
            String sentenceColl = textProcessing.sentenceDetector(dataFile);
            facadePersistence.createSentencesFile(facadePersistence.getFileName(pathFile), sentenceColl);
        }
    }
    
    public String unescapeHtml( String data ){
        StringBuilder unescape = new StringBuilder();
        unescape.append(StringEscapeUtils.unescapeHtml4(data));
        return unescape.toString();
    }
    

    public String applyPreprocessRegexExpression(String data){
        return textProcessing.applyRegexExpression(unescapeHtml(data),"regex.preprocess.rule");
    }
    
    
    
    
    
    
    
    
    
}
